"use strict";

const {src, dest, series, parallel} = require("gulp");
const del = require('del');
const cssBase64 = require('gulp-css-base64');
const autoprefixer = require('autoprefixer');
const postcss = require('gulp-postcss');
const cssnano = require('cssnano');
const babel = require('gulp-babel');
const uglify = require("gulp-uglify");
const inlineSource = require('gulp-inline-source-html')
const htmlmin = require('gulp-htmlmin');

const srcPath = `./src/tinymce/plugins/batchUploadImage/`;
const distPath = `./batchUploadImage/`;

var browserify = require('gulp-browserify');

function clean(cb) {
    return del(distPath);
}

function css() {
    return src([`${srcPath}*.css`], {base: srcPath})
        .pipe(cssBase64())
        .pipe(postcss([autoprefixer(), cssnano()]))
        .pipe(dest(distPath));
}

function js() {
    return src(`${srcPath}*.js`)
        .pipe(babel({presets: ["@babel/env"],plugins: ["@babel/plugin-transform-runtime"]}))
        .pipe(browserify())
        .pipe(uglify())
        .pipe(dest(distPath));
}

function html() {
    return src(`${srcPath}index.html`)
        .pipe(inlineSource({
            attribute: false,
            rootpath: distPath,
        }))
        .pipe(htmlmin({
            collapseWhitespace: true, // 移除换行符
            removeComments: true, // 移除HTML注释
        }))
        .pipe(dest(distPath));
}

function remove() {
    return del([`${distPath}main.css`, `${distPath}main.js`]);
}

exports.default = series(clean, css, js, html, remove);
